package me.lc.linklink.sprite;

import me.lc.linklink.core.GameMap;
import me.lc.linklink.core.GameSystem;
import me.lc.linklink.entity.Location;

import com.wiyun.engine.actions.Action.Callback;
import com.wiyun.engine.actions.Animate;
import com.wiyun.engine.nodes.Animation;
import com.wiyun.engine.nodes.Animation.IAnimationCallback;
import com.wiyun.engine.nodes.Node;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYPoint;
import com.wiyun.engine.types.WYRect;
import com.wiyun.engine.utils.ResolutionIndependent;

public class LLKBlock extends Node implements IAnimationCallback {
	int blockItemIndex;
	Location location;
	Sprite blockSprite;
	Sprite animationSprite;

	public LLKBlock(int itemIndex) {
		blockItemIndex = itemIndex;
		this.setAnchorPercent(0, 0);
	}

	private static int texColumns = 10;

	public void updateBlock() {
		if (blockItemIndex >= 0) {
			int rowNum = blockItemIndex / texColumns;
			int columnNum = blockItemIndex % texColumns;
			WYRect rect = ResolutionIndependent.resolve(WYRect.make(columnNum
					* GameSystem.BLOCK_SIZE, rowNum * GameSystem.BLOCK_SIZE,
					GameSystem.BLOCK_SIZE, GameSystem.BLOCK_SIZE));
			blockSprite = Sprite.make(GameSystem.llkTex, rect);
			blockSprite.setAnchorPercent(0, 0);
			blockSprite.setPosition(0, 0);
			this.addChild(blockSprite);
			blockSprite.autoRelease();
		} else {
			if (blockSprite != null) {
				blockSprite.setVisible(false);
			}
		}
	}

	/**
	 * 爆炸动画
	 */
	public void explodeNode() {
		animationSprite = Sprite.make(GameSystem.cloudTex,
				ResolutionIndependent.resolve(WYRect.make(0, 0,
						GameSystem.BLOCK_SIZE, GameSystem.BLOCK_SIZE)));
		animationSprite.setAnchorPercent(0, 0);
		animationSprite.autoRelease(true);
		animationSprite.setPosition(0, 0);
		addChild(animationSprite);
		Animation anim = new Animation(0);
		anim.addFrame(0.04f, frameAt(0), frameAt(1), frameAt(2), frameAt(3),
				frameAt(4), frameAt(5), frameAt(6));
		anim.setCallback(this);
		Animate a = (Animate) Animate.make(anim).autoRelease();
		a.setCallback(new ExplodeCallback());
		animationSprite.runAction(a);
	}

	private WYRect frameAt(int x) {
		return ResolutionIndependent.resolve(WYRect.make(x
				* GameSystem.BLOCK_SIZE, 0, GameSystem.BLOCK_SIZE,
				GameSystem.BLOCK_SIZE));
	}

	public int getBlockItemIndex() {
		return blockItemIndex;
	}

	public void setBlockItemIndex(int blockItemIndex) {
		this.blockItemIndex = blockItemIndex;
		updateBlock();
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		if (blockItemIndex >= 0) {
			this.location = location;
			float imageX = ResolutionIndependent
					.resolveDp(GameSystem.Margin_Side + GameSystem.BLOCK_SIZE
							* location.getColumn());
			float imageY = GameSystem.Start_Height
					+ ResolutionIndependent.resolveDp(GameSystem.BLOCK_SIZE
							* location.getRow());
			WYPoint point = WYPoint.make(imageX, imageY);
			this.setPosition(point);
		}
	}

	@Override
	public void onAnimationEnded(int arg0) {
		if (animationSprite != null) {
			animationSprite.setVisible(false);
		}
	}

	@Override
	public void onAnimationFrameChanged(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	class ExplodeCallback implements Callback {

		@Override
		public void onStart(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStop(int arg0) {
			// 通知GameMap
			GameMap.moveNode(location.getRow(), location.getColumn());
		}

		@Override
		public void onUpdate(int arg0, float arg1) {
			// TODO Auto-generated method stub

		}
	}
}
